package com.lcc.lynx.jdbc.qw;

import com.lcc.lynx.jdbc.enums.Operator;
import com.lcc.lynx.jdbc.enums.QwMethod;
import com.lcc.lynx.jdbc.pojo.TablePojo;

public class QueryMethods {

    public static QueryWrapper select(Column... columns) {
        return new QueryWrapper().select(columns);
    }

    public static QueryWrapper select(String columnName) {
        return new QueryWrapper().select(new Column(columnName));
    }

    public static QueryWrapper select() {
        return new QueryWrapper().select(new Column("*"));
    }

    public static QueryWrapper select1() {
        return new QueryWrapper().select(new Column("1"));
    }

    public static Column column(String columnName){
        return new Column(columnName);
    }

    public static ColumnCondition distinct(){
        return new ColumnCondition("", QwMethod.DISTINCT, null);
    }
    public static ColumnCondition distinct(Column column) {
        return new ColumnCondition(column.getColumnName(), QwMethod.DISTINCT_COLUMN, column.getTableDef());
    }

    public static ColumnCondition subString(Column column, int start, int length) {
        return new ColumnCondition(column.getColumnName(), QwMethod.SUBSTRING, column.getTableDef(), start, length);
    }

    public static ColumnCondition replace(Column column, String oldValue, String newValue) {
        return new ColumnCondition(column.getColumnName(), QwMethod.REPLACE, column.getTableDef(), oldValue, newValue);
    }

    public static ColumnCondition left(Column column, int length) {
        return new ColumnCondition(column.getColumnName(), QwMethod.LEFT, column.getTableDef(), length);
    }

    public static ColumnCondition right(Column column, int length) {
        return new ColumnCondition(column.getColumnName(), QwMethod.RIGHT, column.getTableDef(), length);
    }

    public static ColumnCondition length(Column column) {
        return new ColumnCondition(column.getColumnName(), QwMethod.LENGTH, column.getTableDef());
    }

    public static ColumnCondition concat(Column column, String value) {
        return new ColumnCondition(column.getColumnName(), QwMethod.CONCAT, column.getTableDef(), value);
    }

    public static ColumnCondition strPos(Column column, String value) {
        return new ColumnCondition(column.getColumnName(), QwMethod.STRPOS, column.getTableDef(), value);
    }

    public static ColumnCondition sum(ColumnCondition columnCondition) {
        columnCondition.setQwMethod(QwMethod.SUM);
        return columnCondition;
    }

    public static ColumnCondition sum(Column column) {
        return new ColumnCondition(column.getColumnName(), QwMethod.SUM, column.getTableDef());
    }

    public static ColumnCondition avg(ColumnCondition columnCondition) {
        columnCondition.setQwMethod(QwMethod.AVG);
        return columnCondition;
    }

    public static ColumnCondition avg(Column column) {
        return new ColumnCondition(column.getColumnName(), QwMethod.AVG, column.getTableDef());
    }

    public static ColumnCondition max(ColumnCondition columnCondition) {
        columnCondition.setQwMethod(QwMethod.MAX);
        return columnCondition;
    }

    public static ColumnCondition max(Column column) {
        return new ColumnCondition(column.getColumnName(), QwMethod.MAX, column.getTableDef());
    }

    public static ColumnCondition min(ColumnCondition columnCondition) {
        columnCondition.setQwMethod(QwMethod.MIN);
        return columnCondition;
    }

    public static ColumnCondition min(Column column) {
        return new ColumnCondition(column.getColumnName(), QwMethod.MIN, column.getTableDef());
    }

    public static ColumnCondition abs(Column column) {
        return new ColumnCondition(column.getColumnName(), QwMethod.ABS, column.getTableDef());
    }

    public static ColumnCondition count(String columnName) {
        return new ColumnCondition(columnName, QwMethod.COUNT, null);
    }

    public static ColumnCondition count(Column column) {
        return new ColumnCondition(column.getColumnName(), QwMethod.COUNT, column.getTableDef());
    }

    public static QueryCondition exists(QueryWrapper queryWrapper){
        return new QueryCondition(Operator.EXISTS, queryWrapper);
    }

    public static QueryCondition nExists(QueryWrapper queryWrapper){
        return new QueryCondition(Operator.NOT_EXISTS, queryWrapper);
    }

    public static QueryCondition exists(String sql){
        return new QueryCondition(Operator.EXISTS, new QueryWrapper(sql));
    }

    public static QueryCondition nExists(String sql){
        return new QueryCondition(Operator.NOT_EXISTS, new QueryWrapper(sql));
    }

    public static QueryCondition ifTest(boolean test, QueryCondition queryCondition){
        return test ? queryCondition : null;
    }
    public static QueryCondition recursive(QueryWrapper qw, Column pid, Column id){
        QueryWrapper qw2 = new QueryWrapper();
        if(qw.getTableJoinList().isEmpty()){
            qw.getTableJoinList().add(new TableJoin(pid.getTableDef()));
        }
        qw2.setColumnList(qw.getColumnList());
        qw2.getTableJoinList().add(new TableJoin(pid.getTableDef()));
        qw2.getTableJoinList().get(0).setAs("t");
        qw2.getTableJoinList().add(new TableJoin("tree", "r", TableJoin.JOIN));
        qw2.on(pid.tAs("t").eq(id.tAs("r")));
        qw2.getTableAsMap().put(qw2.getTableJoinList().get(0).getTableName(), "t");
        if(qw.getSonQueryConditionList() != null){
            qw2.getAndQueryConditionList().addAll(qw.getSonQueryConditionList());
        }

        return new QueryCondition(Operator.RECURSIVE, qw.unionAll(qw2));
    }
}
